import 'package:common_utils/common_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:xr_base/colors.dart';
import 'package:xr_base/dimens.dart';
import 'package:xr_base/func_utils.dart';
import 'package:xr_base/image_constants.dart';
import 'package:xr_base/styles.dart';
import 'package:xr_base/view_util.dart';

class HomeCommonWidget extends StatelessWidget {
  final int? userId;
  final String? title;
  final String? url;
  final String? glamour;
  final String? signature;
  final String? videoCallPrice;
  final String onLineState;
  final ValueChanged<int?>? onTap;
  final ValueChanged<int?>? onStatusTap;

  const HomeCommonWidget(
      {Key? key,
      this.userId,
      this.title,
      this.url,
      this.glamour,
      this.signature,
      this.videoCallPrice,
      required this.onLineState,
      this.onTap,
      this.onStatusTap})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        if (onTap != null) {
          onTap!(userId);
        }
      }.throttleWithTimeout(timeout: 1000),
      child: Container(
        width: double.infinity,
        color: Colours.white_FFFFFF,
        padding: EdgeInsets.only(
          left: Dimens.dn_15,
          right: Dimens.dn_15,
        ),
        child: Column(
          children: [
            Gaps.getVGap(Dimens.dn_16),
            Row(
              children: [
                Stack(
                  alignment: Alignment.bottomCenter,
                  children: [
                    Container(
                      // color: Colours.yellow_ED9C3F,
                      padding: EdgeInsets.only(bottom: Dimens.dn_5),
                      child: ClipRRect(
                        child: cachedImage(
                          url ?? '',
                          width: Dimens.dn_80,
                          height: Dimens.dn_80,
                        ),
                        borderRadius: BorderRadius.circular(Dimens.dn_80),
                      ),
                    ),
                    if (onLineState.toString() == '0')
                      Positioned(
                          bottom: 9,
                          right: 10,
                          child: Container(
                            width: 10,
                            height: 10,
                            decoration: BoxDecoration(
                              color: Colours.gray_B8B8B8,
                              borderRadius: BorderRadius.circular(Dimens.dn_5),
                            ),
                          )),
                    if (onLineState.toString() == '1')
                      Positioned(
                          bottom: 9,
                          right: 10,
                          child: Container(
                            width: 10,
                            height: 10,
                            decoration: BoxDecoration(
                              color: Colours.green_08D07C,
                              borderRadius: BorderRadius.circular(Dimens.dn_5),
                            ),
                          )),
                    if (onLineState.toString() == '2')
                      Container(
                        decoration: BoxDecoration(
                          color: Colours.red_F8614B,
                          borderRadius: BorderRadius.circular(Dimens.dn_8),
                        ),
                        padding: EdgeInsets.symmetric(vertical: Dimens.dn_2, horizontal: Dimens.dn_6),
                        child: Text(
                          '聊天室中',
                          style: TextStyles.ts_8_w500_FFFFFF,
                        ),
                      ),
                  ],
                ),
                Gaps.getHGap(Dimens.dn_13),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        children: [
                          /// 文本超过宽度, 截取
                          Flexible(
                              child: Text(
                            title ?? '',
                            style: TextStyles.ts_18_W5OO_333333,
                            overflow: TextOverflow.ellipsis,
                          )),
                          Gaps.getHGap(Dimens.dn_5),
                          Container(
                            padding: EdgeInsets.symmetric(vertical: Dimens.dn_2, horizontal: Dimens.dn_5),
                            decoration: BoxDecoration(
                              color: Colours.yellow_FFF2DE,
                              borderRadius: BorderRadius.circular(Dimens.dn_2),
                            ),
                            child: Row(
                              children: [
                                localImage(ImageConstants.ico_huo, width: Dimens.dn_9, height: Dimens.dn_10),
                                Gaps.getHGap(Dimens.dn_2),
                                Text(
                                  '魅力值：${glamour ?? 0}',
                                  style: TextStyles.ts_11_w400_yellow_BB6F32,
                                  overflow: TextOverflow.ellipsis,
                                )
                              ],
                            ),
                          )
                        ],
                      ),
                      Gaps.getVGap(Dimens.dn_6),
                      localImage(ImageConstants.ico_maohao, width: Dimens.dn_13, height: Dimens.dn_8),
                      Container(
                        margin: EdgeInsets.only(left: Dimens.dn_9),
                        child: Text(
                          signature ?? '',
                          style: TextStyles.ts_12_w400_666666,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                      Gaps.getVGap(Dimens.dn_10),
                      Row(
                        children: [
                          Expanded(
                              child: Row(
                            crossAxisAlignment: CrossAxisAlignment.end,
                            children: [
                              Text(
                                videoCallPrice ?? '300',
                                style: TextStyles.ts_16_W700_red_F63013,
                                overflow: TextOverflow.ellipsis,
                              ),
                              Text(
                                '金币/每分钟',
                                style: TextStyles.ts_11_W500_red_F63013,
                                overflow: TextOverflow.ellipsis,
                              ),
                            ],
                          )),
                          _statusWidget(),
                        ],
                      )
                    ],
                  ),
                ),
              ],
            ),
            Gaps.getVGap(Dimens.dn_20),
            Dividers.iec_0_5_DDDDDD
          ],
        ),
      ),
    );
  }

  _statusWidget() {
    var _tempTitle = '进入聊天室';
    var _tempBackColor = Colours.gray_B8B8B8;
    var _tempTextStyle = TextStyles.ts_13_w500_FFFFFF;
    var _tempImagePath = ImageConstants.ico_shiping_r_b;

    if (ObjectUtil.isNotEmpty(onLineState)) {
      if (onLineState.toString() == '0') {
        /// 离线
        _tempTitle = '进入聊天室';
        _tempBackColor = Colours.gray_B8B8B8;
        _tempTextStyle = TextStyles.ts_13_w500_FFFFFF;
        _tempImagePath = ImageConstants.ico_shipin_w_b;
      } else if (onLineState.toString() == '1') {
        /// 在线
        _tempTitle = '进入聊天室';
        _tempBackColor = Colours.green_08D07C;
        _tempTextStyle = TextStyles.ts_13_w500_FFFFFF;
        _tempImagePath = ImageConstants.ico_shipin_w_b;
      } else {
        /// 繁忙
        _tempTitle = '视频中';
        _tempBackColor = Colours.red_253226222;
        _tempTextStyle = TextStyles.ts_13_w500_red_2489775;
        _tempImagePath = ImageConstants.ico_shiping_r_b;
      }
    }

    return GestureDetector(
      onTap: () {
        if (onStatusTap != null) {
          onStatusTap!(userId);
        }
      }.throttleWithTimeout(timeout: 1000),
      child: Container(
        padding: EdgeInsets.symmetric(vertical: Dimens.dn_8),
        decoration: BoxDecoration(
          color: _tempBackColor,
          borderRadius: BorderRadius.circular(Dimens.dn_16),
        ),
        width: Dimens.dn_105,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            localImage(_tempImagePath, width: Dimens.dn_16, height: Dimens.dn_12),
            Gaps.getHGap(Dimens.dn_4),
            Text(
              _tempTitle,
              style: _tempTextStyle,
              overflow: TextOverflow.ellipsis,
            ),
          ],
        ),
      ),
    );
  }
}
